﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace LiveWriterBackupWPF
{
    public class RestoreMethods
    {
        private string _tempFolder = string.Format(@"{0}SJL\", Path.GetTempPath());
        public RestoreMethods() { }

        private void CheckTempDir()
        {
            if (!Directory.Exists(_tempFolder))
            {
                Directory.CreateDirectory(_tempFolder);
            }
        }

        public void RestoreBlogSettings()
        {
            Xml2Reg.Import(_tempFolder + @"BlogSettings.xml");
        }

        public void RestoreBlogTemplates()
        {
            string blogtemplatespath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + @"\Windows Live Writer\blogtemplates";
            if (Directory.Exists(blogtemplatespath))
            {
                Directory.Delete(blogtemplatespath, true);
            }
            CopyFolders(_tempFolder + @"blogtemplates", blogtemplatespath);
        }

        public void RestoreLinkGlossary()
        {
            string linkglossarypath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + @"\Windows Live Writer\LinkGlossary";
            if (Directory.Exists(linkglossarypath))
            {
                Directory.Delete(linkglossarypath, true);
            }
            CopyFolders( _tempFolder + @"LinkGlossary", linkglossarypath);
        }

        public void RestoreRecentPosts()
        {
            string myweblogpostspath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + @"\My Weblog Posts";
            if (Directory.Exists(myweblogpostspath + @"\Recent Posts"))
            {
                Directory.Delete(myweblogpostspath + @"\Recent Posts", true);
            }
            CopyFolders(_tempFolder + @"My Weblog Posts\Recent Posts", myweblogpostspath + @"\Recent Posts");
        }

        public void RestoreDrafts()
        {
            string myweblogpostspath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + @"\My Weblog Posts";
            if (Directory.Exists(myweblogpostspath + @"\Drafts"))
            {
                Directory.Delete(myweblogpostspath + @"\Drafts", true);
            }
            CopyFolders(_tempFolder + @"My Weblog Posts\Drafts", myweblogpostspath + @"\Drafts");
        }

        public void RestorePlugins()
        {
            string pluginsfolder = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles) + @"\Windows Live\Writer\Plugins";
            if (Directory.Exists(pluginsfolder))
            {
                Directory.Delete(pluginsfolder, true);
            }
            CopyFolders(_tempFolder + @"Plugins\Plugins", pluginsfolder);
        }

        private static void CopyFolders(string source, string destination)
        {
            if (Directory.Exists(source))
            {
                DirectoryInfo di = new DirectoryInfo(source);
                if (!Directory.Exists(destination))
                {
                    Directory.CreateDirectory(destination);
                }
                foreach (DirectoryInfo d in di.GetDirectories())
                {
                    string newDir = Path.Combine(destination, d.Name);
                    Directory.CreateDirectory(newDir);

                    CopyFolders(d.FullName, newDir);
                }
                CopyFiles(source, destination);
            }
        }

        private static void CopyFiles(string source, string destination)
        {
            if (Directory.Exists(source))
            {
                DirectoryInfo di = new DirectoryInfo(source);
                FileInfo[] files = di.GetFiles();

                foreach (FileInfo f in files)
                {
                    f.IsReadOnly = false;
                    string sourceFile = f.FullName;
                    string destFile = Path.Combine(destination, f.Name);
                    if (!File.Exists(destFile))
                    {
                        File.Copy(sourceFile, destFile);
                    }
                }
            }
        }
    }
}
